package com.fly.toolbox.config;

import com.fly.toolbox.bean.TaskInfo;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ApplicationProperties {
    private static ApplicationProperties instance;

    private String user;
    private String[] versions;
    private List<String> history;
    private String baseClassPath;
    private String exportFilePath;

    // FileSelector的容器
    private Map<String, DefaultListModel<VirtualFile>> vfsMap = new ConcurrentHashMap<>();

    private ApplicationProperties() {
        // 私有构造函数，防止外部实例化
    }

    public static ApplicationProperties getInstance() {
        if (instance == null) {
            synchronized (ApplicationProperties.class) {
                if (instance == null) {
                    instance = new ApplicationProperties();
                    instance.init();
                }
            }
        }
        return instance;
    }

    public List<String> getHistory() {
        return history;
    }

    public String[] getHistoryAsArray() {
        if (history == null) {
            return new String[0];
        }
        String[] array = new String[history.size()];
        for (int i = 0; i < history.size(); i++) {
            array[history.size() - i - 1] = history.get(i);
        }
        return array;
    }

    public void setHistory(List<String> history) {
        this.history = history;
    }

    public void addHistory(String text, TaskInfo taskInfo) {
        if (this.history == null || this.history.isEmpty()) {
            history = new ArrayList<>();
        }
        Iterator<String> it = history.iterator();
        while (it.hasNext()) {
            String his = it.next();
            if (his.contains(taskInfo.getTaskId()) && his.contains(taskInfo.getOrderId())) {
                it.remove();
                break;
            }
        }
        if (history.size() == 10) {
            history.remove(0);
        }
        this.history.add(text);

        PropertiesComponent properties = PropertiesComponent.getInstance();
        properties.setValue("history", String.join("~", history));
    }

    public void init() {
        PropertiesComponent properties = PropertiesComponent.getInstance();
        String _user = properties.getValue("user");
        String _versions = properties.getValue("versions");
        String _history = properties.getValue("history");
        String _baseClassPath = properties.getValue("baseClassPath");
        String _exportFilePath = properties.getValue("exportFilePath");

        if (StringUtils.isNotEmpty(_user)) {
            user = _user.trim();
        } else {
            user = "xxx";
        }
        if (StringUtils.isNotEmpty(_versions)) {
            versions = _versions.split(",");
        } else {
            versions = null;
        }
        if (versions == null || versions.length == 0) {
            versions = new String[12];
            for (int i = 0; i < versions.length; i++) {
                LocalDate currentDate = LocalDate.now();
                int year = currentDate.getYear();
                if (i < 10) {
                    versions[i] = "BTA5.0V" + year + "01.0" + i + ".000";
                } else {
                    versions[i] = "BTA5.0V" + year + "01." + i + ".000";
                }
            }
        }
        if (StringUtils.isNotEmpty(_history)) {
            history = new ArrayList<>(Arrays.asList(_history.split("~")));
        } else {
            history = null;
        }

        this.baseClassPath = _baseClassPath;
        this.exportFilePath = _exportFilePath;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String[] getVersions() {
        return versions;
    }

    public void setVersions(String[] versions) {
        this.versions = versions;
    }

    public String getBaseClassPath() {
        return baseClassPath;
    }

    public void setBaseClassPath(String baseClassPath) {
        this.baseClassPath = baseClassPath;
    }

    public String getExportFilePath() {
        return exportFilePath;
    }

    public void setExportFilePath(String exportFilePath) {
        this.exportFilePath = exportFilePath;
    }

    public Map<String, DefaultListModel<VirtualFile>> getVfsMap() {
        return vfsMap;
    }

    public void setVfsMap(Map<String, DefaultListModel<VirtualFile>> vfsMap) {
        this.vfsMap = vfsMap;
    }

    public DefaultListModel<VirtualFile> getModel(String id) {
        return this.vfsMap.computeIfAbsent(id, k -> new DefaultListModel<>());
    }

    public void removeAllElements(String id) {
        DefaultListModel<VirtualFile> model = getModel(id);
        getModel(id).removeAllElements();
    }

    public void removeElements(String id, int[] elementIds) {
        DefaultListModel<VirtualFile> model = getModel(id);
        for (int i = elementIds.length - 1; i >= 0; i--) {
            VirtualFile elementAt = model.getElementAt(i);
            model.remove(elementIds[i]);
        }
    }

    public void addFile(String id, VirtualFile file) {
        DefaultListModel<VirtualFile> model = getModel(id);
        Iterator<VirtualFile> iterator = model.elements().asIterator();
        while (iterator.hasNext()) {
            VirtualFile file1 = iterator.next();
            if (file.getPath().equals(file1.getPath())) {
                return;
            }
        }
        model.addElement(file);
    }

}
